﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MKAdminCore.Controllers;
using MKAdminCore.DTO.CusAttribute;
using MKAdminCore.DTO.Web.Common;
using MKAdminCore.DTO.Web.TableData.BasicTable;
using MKAdminCore.IService.Web.TableData;
using MKAdminCore.ToolKit.Config;

namespace MKAdminCore.Areas.TableData.Controllers
{
    [Area("TableData")]
    [Route("TableData/[controller]/[action]")]
    public class BasicTableController : BaseMvcController
    {
        private IBasicTableService _iBasicTableService;

        public BasicTableController(IBasicTableService iBasicTableService)
        {
            _iBasicTableService = iBasicTableService;
        }

        public IActionResult Index()
        {
            return View(PageViewFilesConfig.BasicTableIndex);
        }

        [HttpPost]
        public IActionResult List(GetBasicTableListParameter parameter)
        {
            var result = _iBasicTableService.List(parameter);

            return Ok(result);
        }

        [HttpPost]
        public async Task<IActionResult> Add(AddBasicTableParam parameter)
        {
            var result = new HttpResponseResult();
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }
            var userInfo = base.GetUserInfo();
            result = await _iBasicTableService.Add(parameter, userInfo);
            return Ok(result);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(EditBasicTableParam parameter)
        {
            var result = new HttpResponseResult();
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }
            result = await _iBasicTableService.Edit(parameter);
            return Ok(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Del(DelBasicTableParam parameter)
        {
            var result = new HttpResponseResult();
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }
            result = await _iBasicTableService.Del(parameter);

            return Ok(result);

        }
    }
}